package com.zfp.hs.service;

import com.zfp.core.service.BaseService;
import com.zfp.hs.dao.OrderDao;
import com.zfp.hs.entity.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import java.math.BigDecimal;

@Transactional
@Repository("orderService")
public class OrderService extends BaseService<Order> {
	@Autowired
	private OrderDao orderDao;


    @Rollback
    public boolean save(Order order){
        int row = 0;
        if(order.getId() != null){
            row=orderDao.update(order);
            if(row >0){
                Order oldorder = new Order();
                oldorder.setTimelineId(Integer.parseInt(order.getOldtimelineId()));
                row = orderDao.update("Order.updateQuatoPlus",oldorder);
                if(row >0){
                    row = orderDao.update("Order.updateQuatoDec",order);
                }
            }
        }else{
            row = orderDao.insert(order);
            if(row >0){
                row = orderDao.update("Order.updateQuatoDec",order);
            }
        }
        boolean flag=row>=1?true:false;
        return flag;
    }

    @Rollback
    public boolean savefeedback(Order order){
        int row = 0;
        if(order.getId() != null){
            row=orderDao.update(order);

        }
        boolean flag=row>=1?true:false;
        return flag;
    }

    public boolean deleteById(Order order){
        int row = 0;
        if(order.getId() != null ){
            row=orderDao.delete(order);
            if(row >0){
                row = orderDao.update("Order.updateQuatoPlus",order);
            }
        }
        boolean flag=row>=1?true:false;
        return flag;
    }

    public void preOrder(Order order, Model model){
        Order dcinfo = orderDao.getObject("Order.getOrderCountByDcid",order);
        Order ordinfo = orderDao.getObject("Order.getOrderCountByTlid",order);
        String outordertime = "0";
        String inordertime = "0";
        //预约时间内排队时间计算
        inordertime = new BigDecimal(ordinfo.getSumorder()).multiply(new BigDecimal(dcinfo.getDiagnosetime())).toString();
        //预约时间外排队时间计算
        Integer lasti = Integer.parseInt(order.getImitatepeople());//当现场挂号人数为奇数时剩下的1个人单独处理
        Integer lastj = Integer.parseInt(ordinfo.getSumorder());//当现场挂号人数少于网上挂号的2倍时对多出的网上挂号的单独处理
        for(int i= Integer.parseInt(order.getImitatepeople()), j =Integer.parseInt(ordinfo.getSumorder()) ;i>1;){
            if(j>0){
                outordertime = new BigDecimal(dcinfo.getDiagnosetime()).multiply(new BigDecimal("3")).add(new BigDecimal(outordertime.trim())).toString();
                j=j-1;
                lastj=j;
                i=i-2;
                lasti = i;
            }else{
                if(lasti>=2){
                    lasti = 2;
                }
                break;
            }
        }
        outordertime = new BigDecimal(dcinfo.getDiagnosetime()).multiply(new BigDecimal(lasti).add(new BigDecimal(lastj))).add(new BigDecimal(outordertime.trim())).toString();
        model.addAttribute("preordersum",ordinfo.getSumorder());
        model.addAttribute("avgtime",dcinfo.getDiagnosetime());
        model.addAttribute("imitatepeople",order.getImitatepeople());
        model.addAttribute("inordertime",inordertime);
        model.addAttribute("outordertime",outordertime);
    }

}